package com.imiku.controller;

import com.alibaba.fastjson.JSONObject;
import com.imiku.model.UserInfo;
import com.imiku.service.UserInfoService;
import com.imiku.utils.JsonUtils;
import com.imiku.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhaoxinguo on 2017/9/13.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 获取用户列表
     *
     * @return
     */
    @RequestMapping("/findByPage")
    public String findByPage(PageVo pageVo) {
        Page<UserInfo> users = userInfoService.findByPage(pageVo);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "success");
        jsonObject.put("page", users);
        return jsonObject.toJSONString();
    }


    @RequestMapping("/details/{userId}")
    public String details(@PathVariable int userId) {
        UserInfo userInfo = userInfoService.findByUserId(userId);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        jsonObject.put("info", userInfo);
        return jsonObject.toJSONString();
    }


    @RequestMapping("/save")
    public String save(UserInfo userInfo) {
        userInfoService.save(userInfo);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        return jsonObject.toJSONString();
    }

    @RequestMapping("/update")
    public String update(UserInfo userInfo) {
        userInfoService.update(userInfo);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        return jsonObject.toJSONString();
    }

}
